package com.qcby.chatservice.socket.lister;

import com.qcby.chatservice.common.enums.GroupConst;
import lombok.extern.slf4j.Slf4j;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.core.intf.Packet;
import org.tio.websocket.common.WsResponse;
import org.tio.websocket.common.WsSessionContext;


/**
 * 用于监听socket事件
 *
 * @author cong.zhen
 * @date 2022/12/30
 */
@Slf4j
public class WsServerAioListener extends org.tio.websocket.server.WsServerAioListener {

    public static final WsServerAioListener me = new WsServerAioListener();

    private WsServerAioListener() {

    }

    /**
     * 创建连接之后执行
     *
     * @param channelContext 信道环境
     * @param isConnected    连接
     * @param isReconnect    是连接
     * @throws Exception 异常
     */
    @Override
    public void onAfterConnected(ChannelContext channelContext, boolean isConnected, boolean isReconnect) throws Exception {
        super.onAfterConnected(channelContext, isConnected, isReconnect);
        log.info("用户id:{},上线了",channelContext.userid);
    }

    /**
     * 发送之后执行，一般操作就是操作数据库插入数据
     *
     * @param channelContext 信道环境
     * @param packet         包
     * @param isSentSuccess  发送成功
     * @throws Exception 异常
     */
    @Override
    public void onAfterSent(ChannelContext channelContext, Packet packet, boolean isSentSuccess) throws Exception {
        super.onAfterSent(channelContext, packet, isSentSuccess);
        log.info("onAfterSent\r\n{}\r\n{}", packet.logstr(), channelContext);
    }

    /**
     * 关闭之前执行
     *
     * @param channelContext 信道环境
     * @param throwable      throwable
     * @param remark         备注
     * @param isRemove       是删除
     * @throws Exception 异常
     */
    @Override
    public void onBeforeClose(ChannelContext channelContext, Throwable throwable, String remark, boolean isRemove) throws Exception {
        super.onBeforeClose(channelContext, throwable, remark, isRemove);
        if (log.isInfoEnabled()) {
            log.info("onBeforeClose\r\n{}", channelContext);
        }
        WsSessionContext wsSessionContext = (WsSessionContext) channelContext.get();
        if (wsSessionContext != null && wsSessionContext.isHandshaked()) {
            int count = Tio.getAllChannelContexts(channelContext.tioConfig).getObj().size();
            String msg = channelContext.getClientNode().toString() + " 离开了，现在共有【" + count + "】人在线";
            //用tio-websocket，服务器发送到客户端的Packet都是WsResponse
            WsResponse wsResponse = WsResponse.fromText(msg, "utf-8");
            //群发
            Tio.sendToGroup(channelContext.tioConfig, GroupConst.GROUP_ID, wsResponse);
        }
    }

    @Override
    public void onAfterDecoded(ChannelContext channelContext, Packet packet, int packetSize) throws Exception {
        super.onAfterDecoded(channelContext, packet, packetSize);
        if (log.isInfoEnabled()) {
            log.info("onAfterDecoded\r\n{}\r\n{}", packet.logstr(), channelContext);
        }
    }

    @Override
    public void onAfterReceivedBytes(ChannelContext channelContext, int receivedBytes) throws Exception {
        super.onAfterReceivedBytes(channelContext, receivedBytes);
        if (log.isInfoEnabled()) {
            log.info("onAfterReceivedBytes\r\n{}", channelContext);
        }
    }

    @Override
    public void onAfterHandled(ChannelContext channelContext, Packet packet, long cost) throws Exception {
        super.onAfterHandled(channelContext, packet, cost);
        if (log.isInfoEnabled()) {
            log.info("onAfterHandled\r\n{}\r\n{}", packet.logstr(), channelContext);
        }
    }

}
